import {
  Entity,
  PrimaryGeneratedColumn,
  Column,
  CreateDateColumn,
  ManyToOne,
  JoinColumn,
  Unique,
} from 'typeorm';
import { User } from '../../users/entities/user.entity';
import { Tool } from '../../tools/entities/tool.entity';

/**
 * 收藏实体
 */
@Entity('favorites')
@Unique(['userId', 'toolId'])
export class Favorite {
  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  userId: number;

  @Column()
  toolId: number;

  @CreateDateColumn()
  createdAt: Date;

  // 关联关系
  @ManyToOne(() => User, (user) => user.favorites, {
    onDelete: 'CASCADE',
  })
  @JoinColumn({ name: 'userId' })
  user: User;

  @ManyToOne(() => Tool, (tool) => tool.favorites, {
    onDelete: 'CASCADE',
  })
  @JoinColumn({ name: 'toolId' })
  tool: Tool;
}







